// import { http } from '@kit.NetworkKit';
//
// export class RequestUtil {
//   public static httpRequest: http.HttpRequest = http.createHttp();
//
//   public static async get(url: string) {
//     let data = await RequestUtil.httpRequest.request(url)
//     console.info('Result:' + data.result);
//     console.info('code:' + data.responseCode);
//     console.info('type:' + JSON.stringify(data.resultType));
//     console.info('header:' + JSON.stringify(data.header));
//     console.info('cookies:' + data.cookies);
//     return data
//   }
//
//   public static async post(url: string, options: http.HttpRequestOptions) {
//     let data = await RequestUtil.httpRequest.request(url, options)
//     console.info('Result:' + data.result);
//     console.info('code:' + data.responseCode);
//     console.info('type:' + JSON.stringify(data.resultType));
//     console.info('header:' + JSON.stringify(data.header));
//     console.info('cookies:' + data.cookies);
//     return data
//   }
// }
// NetworkUtil.ets
import { http } from '@kit.NetworkKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

export class HttpClient {
  private httpRequest: http.HttpRequest;

  constructor() {
    this.httpRequest = http.createHttp();
  }

  get(url: string, callback: AsyncCallback<http.HttpResponse>) {
    this.httpRequest.request(url, {
      method: http.RequestMethod.GET,
      priority: 1,
      connectTimeout: 60000,
      readTimeout: 60000,
      header: { 'Content-Type': 'application/json' },
      extraData: {}
    }, callback);
  }

  post(url: string, data: Object, callback: AsyncCallback<http.HttpResponse>) {
    this.httpRequest.request(url, {
      method: http.RequestMethod.POST,
      priority: 1,
      connectTimeout: 60000,
      readTimeout: 60000,
      header: { 'Content-Type': 'application/json' },
      extraData: data
    }, callback);
  }

  put(url: string, data: Object, callback: AsyncCallback<http.HttpResponse>) {
    this.httpRequest.request(url, {
      method: http.RequestMethod.PUT,
      priority: 1,
      connectTimeout: 60000,
      readTimeout: 60000,
      header: { 'Content-Type': 'application/json' },
      extraData: data
    }, callback);
  }

  delete(url: string, callback: AsyncCallback<http.HttpResponse>) {
    this.httpRequest.request(url, {
      method: http.RequestMethod.DELETE,
      priority: 1,
      connectTimeout: 60000,
      readTimeout: 60000,
      header: { 'Content-Type': 'application/json' },
      extraData: {}
    }, callback);
  }

  // 销毁实例
  public destroy() {
    if (this.httpRequest) {
      this.httpRequest.destroy();
    }
  }
}

//export default new HttpClient();




